package com.sg.service.biz.pbff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.BffJudgeDataSeparationReqDto;
import com.sg.dto.biz.pbff.req.BffQueryDataSeparationDetailReqDto;
import com.sg.dto.biz.pbff.req.BffQueryDataSeparationListReqDto;
import com.sg.dto.biz.pbff.req.BffUpdateDataSeparationReqDto;
import com.sg.dto.biz.pbff.res.BffJudgeDataSeparationRespDto;
import com.sg.dto.biz.pbff.res.BffQueryDataSeparationDetailRespDto;
import com.sg.dto.biz.pbff.res.BffQueryDataSeparationListRespDto;
import com.sg.dto.biz.pbff.res.BffUpdateDataSeparationRespDto;
import com.sg.service.biz.pbff.DeploymentService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.dto.*;
import com.wicket.okroperation.integration.DeploymentService;
import com.wicket.okroperation.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DeploymentServiceImpl implements DeploymentService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.operation.DeploymentService fwOperationDeploymentClient;
    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;

    /**
     * B6判断数据分离(管理)[6938]
     * gen by moon at 9/13/2023, 10:45:05 PM
     */
    @Trace(operationName = "B6判断数据分离(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffJudgeDataSeparationRespDto judgeDataSeparation(BffJudgeDataSeparationReqDto reqDto) {


        //步骤0: D6判断数据分离 - judgeDataSeparation
        JudgeDataSeparationRespDto judgeDataSeparationRespDto = null;
        JudgeDataSeparationReqDto judgeDataSeparationReqDto = new JudgeDataSeparationReqDto();
        if (reqDto != null) {
            judgeDataSeparationReqDto.setOperationSeparatedDataId(reqDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976504_1
        }

        /*D6判断数据分离[6937]   */
        Assert.isNull(judgeDataSeparationReqDto.getOperationSeparatedDataId(), "B6判断数据分离(管理)-D6判断数据分离-分离数据表主键ID不能为空", false);
        judgeDataSeparationRespDto = fwOperationDeploymentClient.judgeDataSeparation(judgeDataSeparationReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        BffJudgeDataSeparationRespDto retData = new BffJudgeDataSeparationRespDto();


        return retData;
    }

    /**
     * B6修改数据分离(管理)[6934]
     * gen by moon at 9/13/2023, 10:57:08 PM
     */
    @Trace(operationName = "B6修改数据分离(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdateDataSeparationRespDto updateDataSeparation(BffUpdateDataSeparationReqDto reqDto) {


        //步骤0: D6修改数据分离 - updateDataSeparation
        UpdateDataSeparationRespDto updateDataSeparationRespDto = null;
        UpdateDataSeparationReqDto updateDataSeparationReqDto = new UpdateDataSeparationReqDto();
        if (reqDto != null) {
            updateDataSeparationReqDto.setPrdTableEncryptionName(reqDto.getPrdTableEncryptionName());//SimpleFieldAssign//sourceId:976501_1
            updateDataSeparationReqDto.setIsNeeddataSeparated(reqDto.getIsNeeddataSeparated());//SimpleFieldAssign//sourceId:976161_1
            updateDataSeparationReqDto.setDataSeparatedStrategyTypeCode(reqDto.getDataSeparatedStrategyTypeCode());//SimpleFieldAssign//sourceId:976162_1
        }

        /*D6修改数据分离[6931]   */

        updateDataSeparationRespDto = fwOperationDeploymentClient.updateDataSeparation(updateDataSeparationReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        BffUpdateDataSeparationRespDto retData = new BffUpdateDataSeparationRespDto();


        return retData;
    }

    /**
     * B6查询数据分离列表(管理)[6935]
     * gen by moon at 9/13/2023, 10:57:10 PM
     */
    @Trace(operationName = "B6查询数据分离列表(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryDataSeparationListRespDto queryDataSeparationList(BffQueryDataSeparationListReqDto reqDto) {


        QueryDataSeparationListRespDto queryDataSeparationListRespDto_1 = null;
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto_1 = null;
//步骤0: D6查询数据分离列表 - queryDataSeparationList
        QueryDataSeparationListRespDto queryDataSeparationListRespDto = null;
        QueryDataSeparationListReqDto queryDataSeparationListReqDto = new QueryDataSeparationListReqDto();
        if (reqDto != null) {
            queryDataSeparationListReqDto.setTableCnName(reqDto.getTableCnName());//SimpleFieldAssign//sourceId:976166_1
            queryDataSeparationListReqDto.setDataSeparatedStatus(reqDto.getDataSeparatedStatus());//SimpleFieldAssign//sourceId:976167_1
        }

        /*D6查询数据分离列表[6932]   */

        queryDataSeparationListRespDto = fwOperationDeploymentClient.queryDataSeparationList(queryDataSeparationListReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryDataSeparationListRespDto_1 = queryDataSeparationListRespDto;

//步骤1: D1-3批量查询就职记录(管理)(公共) - batchQueryInductionRecordCom
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto = null;
        if (queryDataSeparationListRespDto != null) {
            BatchQueryInductionRecordComReqDto batchQueryInductionRecordComReqDto = new BatchQueryInductionRecordComReqDto();
            if (queryDataSeparationListRespDto != null && queryDataSeparationListRespDto.getDataSeparationList() != null && !CollectionUtil.isEmpty(queryDataSeparationListRespDto.getDataSeparationList())) {
                batchQueryInductionRecordComReqDto.setOrgInductionRecordList(queryDataSeparationListRespDto.getDataSeparationList().stream().map(item -> item.getOperationInductionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:976169_1
            }

            /*D1-3批量查询就职记录(管理)(公共)[5024]   */

            batchQueryInductionRecordComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordCom(batchQueryInductionRecordComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            batchQueryInductionRecordComRespDto_1 = batchQueryInductionRecordComRespDto;
        }

        BffQueryDataSeparationListRespDto retData = new BffQueryDataSeparationListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryDataSeparationListRespDto_1 != null && !CollectionUtil.isEmpty(queryDataSeparationListRespDto_1.getDataSeparationList())) {
            for (DataSeparationDto dataSeparationDto : queryDataSeparationListRespDto_1.getDataSeparationList()) {
                BffDataSeparationDto retElm = new BffDataSeparationDto();
                if (queryDataSeparationListRespDto_1 != null) {
                    retElm.setOperationSeparatedDataId(dataSeparationDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:207970_2
                    retElm.setTableId(dataSeparationDto.getTableId());//SimpleFieldAssign//sourceId:207971_2
                    retElm.setTableCnName(dataSeparationDto.getTableCnName());//SimpleFieldAssign//sourceId:207972_2
                    retElm.setTableEnName(dataSeparationDto.getTableEnName());//SimpleFieldAssign//sourceId:207973_2
                    retElm.setOperationInductionId(dataSeparationDto.getOperationInductionId());//SimpleFieldAssign//sourceId:207974_2
                    retElm.setOperateTime(dataSeparationDto.getOperateTime());//SimpleFieldAssign//sourceId:207975_2
                }
                retData.getDataSeparationList().add(retElm);
                if (batchQueryInductionRecordComRespDto_1 != null) {
                    for (OrgInductionRecordDto orgInductionRecordDto : batchQueryInductionRecordComRespDto_1.getOrgInductionRecordList()) {
                        if (orgInductionRecordDto.getInductionRecordId().equals(dataSeparationDto.getOperationInductionId())) {
                            if (batchQueryInductionRecordComRespDto_1 != null) {
                                retElm.setUserId(orgInductionRecordDto.getUserId());//SimpleFieldAssign//sourceId:207976_2
                                retElm.setIdCardName(orgInductionRecordDto.getIdCardName());//SimpleFieldAssign//sourceId:207978_2
                                retElm.setNickName(orgInductionRecordDto.getNickName());//SimpleFieldAssign//sourceId:207979_2
                                retElm.setUserFace(orgInductionRecordDto.getUserFace());//SimpleFieldAssign//sourceId:207977_2
                            }
                        }
                    }
                }

            }
        }//sourceId:976173_1


        retData.setPageNum(queryDataSeparationListRespDto.getPageNum());
        retData.setPageSize(queryDataSeparationListRespDto.getPageSize());
        retData.setTotal(queryDataSeparationListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryDataSeparationListRespDto.getTotalNum()) / Double.valueOf(queryDataSeparationListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B6查询数据分离详情(管理)[6936]
     * gen by moon at 9/13/2023, 10:57:13 PM
     */
    @Trace(operationName = "B6查询数据分离详情(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryDataSeparationDetailRespDto queryDataSeparationDetail(BffQueryDataSeparationDetailReqDto reqDto) {


        QueryDataSeparationDetailRespDto queryDataSeparationDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D6查询数据分离详情 - queryDataSeparationDetail
        QueryDataSeparationDetailRespDto queryDataSeparationDetailRespDto = null;
        QueryDataSeparationDetailReqDto queryDataSeparationDetailReqDto = new QueryDataSeparationDetailReqDto();
        if (reqDto != null) {
            queryDataSeparationDetailReqDto.setOperationSeparatedDataId(reqDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976174_1
        }

        /*D6查询数据分离详情[6933]   */
        Assert.isNull(queryDataSeparationDetailReqDto.getOperationSeparatedDataId(), "B6查询数据分离详情(管理)-D6查询数据分离详情-分离数据表主键ID不能为空", false);
        queryDataSeparationDetailRespDto = fwOperationDeploymentClient.queryDataSeparationDetail(queryDataSeparationDetailReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryDataSeparationDetailRespDto_1 = queryDataSeparationDetailRespDto;

//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (queryDataSeparationDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (queryDataSeparationDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryDataSeparationDetailRespDto.getOperationInductionId());//SimpleFieldAssign//sourceId:976188_1
            }

            /*D1-3查询个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B6查询数据分离详情(管理)-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

        BffQueryDataSeparationDetailRespDto retData = new BffQueryDataSeparationDetailRespDto();
        if (queryDataSeparationDetailRespDto_1 != null) {
            retData.setOperationSeparatedDataId(queryDataSeparationDetailRespDto_1.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976200_1
            retData.setTableId(queryDataSeparationDetailRespDto_1.getTableId());//SimpleFieldAssign//sourceId:976201_1
            retData.setTableCnName(queryDataSeparationDetailRespDto_1.getTableCnName());//SimpleFieldAssign//sourceId:976202_1
            retData.setTableEnName(queryDataSeparationDetailRespDto_1.getTableEnName());//SimpleFieldAssign//sourceId:976203_1
            retData.setToolTableEncryptionName(queryDataSeparationDetailRespDto_1.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:976204_1
            retData.setPrdTableEncryptionName(queryDataSeparationDetailRespDto_1.getPrdTableEncryptionName());//SimpleFieldAssign//sourceId:976205_1
            retData.setEncryptionNameIsSame(queryDataSeparationDetailRespDto_1.getEncryptionNameIsSame());//SimpleFieldAssign//sourceId:976206_1
            retData.setBelongSpaceId(queryDataSeparationDetailRespDto_1.getBelongSpaceId());//SimpleFieldAssign//sourceId:976207_1
            retData.setIsNeeddataSeparated(queryDataSeparationDetailRespDto_1.getIsNeeddataSeparated());//SimpleFieldAssign//sourceId:976208_1
            retData.setDataSeparatedStrategyTypeCode(queryDataSeparationDetailRespDto_1.getDataSeparatedStrategyTypeCode());//SimpleFieldAssign//sourceId:976209_1
            retData.setDataSeparatedStatus(queryDataSeparationDetailRespDto_1.getDataSeparatedStatus());//SimpleFieldAssign//sourceId:976210_1
            retData.setOperationInductionId(queryDataSeparationDetailRespDto_1.getOperationInductionId());//SimpleFieldAssign//sourceId:976211_1
            retData.setOperateTime(queryDataSeparationDetailRespDto_1.getOperateTime());//SimpleFieldAssign//sourceId:976212_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//SimpleFieldAssign//sourceId:976213_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//SimpleFieldAssign//sourceId:976214_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//SimpleFieldAssign//sourceId:976215_1
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//SimpleFieldAssign//sourceId:976216_1
        }


        return retData;
    }
    //
}
